package com.xhwl.centiotdata.handler.visitor.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationFactory;
import com.xhwl.centiotdata.handler.visitor.VisitorIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.haikang.VisitorInfoHaikangDTO;
import com.xhwl.centiotdata.pojo.dto.visitor.VisitorIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IAuthAccountCentService;
import com.xhwl.centiotdata.suppliersystem.haikang.VistorHaikangHandler;
import com.xhwl.centiotdata.utils.Base64Utils;
import com.xhwl.centiotdata.utils.FaceDataUtil;
import com.xhwl.common.dto.cent.visitor.VisitorAuthDTO;
import com.xhwl.common.dto.cent.visitor.VisitorInfoDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.visitor.Follower;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author pengjunming
 * @description
 * @date 2022/6/10 17:02
 */
@Service
@Slf4j
public class VisitorIntegrationHaikangHandler extends VisitorIntegrationHandler {

    @Resource
    private VistorHaikangHandler vistorHaikangHandler;

    @Resource
    private IAuthAccountCentService iAuthAccountCentService;

    @Override
    public void afterPropertiesSet() throws Exception {
        VisitorIntegrationFactory.register(2, this);
    }


    @Override
    public ResultJson createVisitorAuth(VisitorIntegrationConfigDTO configDTO, VisitorAuthDTO authDTO) {
        log.info("创建访客信息:{}", JSONObject.toJSONString(authDTO));
        List<Follower> followerList = authDTO.getFollowerList();
        List<VisitorInfoHaikangDTO.VisitorInfoDTO> followVisitors = new ArrayList<>();

        //设置访客信息
        if (!CollectionUtils.isEmpty(followerList)) {
            followerList.forEach(follower -> {
                VisitorInfoHaikangDTO.VisitorInfoDTO followVisitor = new VisitorInfoHaikangDTO.VisitorInfoDTO();
                followVisitor.setVisitorPhoto(FaceDataUtil.getFaceData(follower.getFaceUrl()));
                followVisitor.setVisitorName(follower.getName());
                followVisitor.setPhoneNo(follower.getPhone());
                followVisitors.add(followVisitor);
            });
        }
        VisitorInfoHaikangDTO.VisitorInfoDTO followVisitor = new VisitorInfoHaikangDTO.VisitorInfoDTO();
        followVisitor.setVisitorPhoto(FaceDataUtil.getFaceData(authDTO.getFaceUrl()));
        followVisitor.setVisitorName(authDTO.getName());
        followVisitor.setPhoneNo(authDTO.getPhone());
        followVisitor.setGender(1);
        followVisitors.add(followVisitor);

        Date authStartTime = authDTO.getAuthStartTime();
        Date authEndTime = authDTO.getAuthEndTime();
        String receptionistId = authDTO.getIntervieweeOpenId();

        //海康日期格式转换
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        String startTime = sdf.format(authStartTime);
        String endTime = sdf.format(authEndTime);

        if (StringUtils.isEmpty(receptionistId)) {
            receptionistId = iAuthAccountCentService.queryOpenId(authDTO.getProjectId(), authDTO.getIntervieweeId(), null, authDTO.getProductId());
        }

        if (StringUtils.isEmpty(receptionistId)) {
            return ResultJson.fail("受防者信息不存在");
        }

        VisitorInfoHaikangDTO vis = VisitorInfoHaikangDTO.builder()
                .visitorInfoList(followVisitors)
                .visitStartTime(startTime)
                .visitEndTime(endTime)
                .receptionistId(receptionistId)
                .visitPurpose(authDTO.getSubjectMatter()).build();
        return vistorHaikangHandler.createVistorAuth(vis, configDTO);
    }

    @Override
    public ResultJson removeVisitorAuth(VisitorIntegrationConfigDTO configDTO, VisitorInfoDTO infoDTO) {

        log.info(JSONObject.toJSONString(infoDTO));
        return ResultJson.success();

    }



}
